const bags = [
    {groupId: "0001", name: "Bag 0001-1"},
    {groupId: "0001", name: "Bag 0001-2"},
    {groupId: "0001", name: "Bag 0001-3"},
    {groupId: "0001", name: "Bag 0001-4"},
    {groupId: "0001", name: "Bag 0001-5"},
    { name: "Bag No group 1"},
    { name: "Bag No group 2"},
    { name: "Bag No group 3"},
    { name: "Bag No group 4"},
    {groupId: "0002", name: "Bag 0002-1"},
    {groupId: "0002", name: "Bag 0002-2"},
    {groupId: "0002", name: "Bag 0002-3"},
    {groupId: "0002", name: "Bag 0002-4"},
    {groupId: "0002", name: "Bag 0002-5"},
    {groupId: "0001", name: "Bag 0001-6"},

]

function groupBagsByGroupId(bags) {
    return bags.reduce((groups, bag) => {
        const groupId = bag.groupId || 'NoN-Group';
        if (!groups[groupId]) {
            groups[groupId] = [];
        }
        groups[groupId].push(bag);
        return groups;
    }, {});
}

// 使用示例
const groupedBags = groupBagsByGroupId(bags);
console.log(groupedBags);
console.log(Object.keys(groupedBags))
console.log(Object.values(groupedBags))